package com.spzx.user.service.impl;

import java.util.List;

import cmo.spzx.user.api.domain.UpdateUserLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cmo.spzx.user.api.domain.UserAddress;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.mapper.UserAddressMapper;
import io.jsonwebtoken.lang.Assert;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import cmo.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-09-28
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService
{
//    @Autowired
//    private UserInfoMapper userInfoMapper;
    @Resource
    UserInfoMapper userInfoMapper;
//    @Autowired
//    private UserAddressMapper userAddressMapper;
    @Resource
    UserAddressMapper userAddressMapper;

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo)
    {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    @Override
    public List<UserAddress> selectUserAddressList(Long userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId,userId));

    }

    @Override
    public Boolean register(UserInfo userInfo) {
//        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("phone", userInfo.getPhone());
//        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        List<UserInfo> userInfos = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getPhone, userInfo.getPhone()));
        Assert.isTrue(userInfos.size()<=0, "手机已经注册");
        userInfo.setUsername(userInfo.getPhone());
        userInfo.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        userInfo.setStatus(1);
        userInfo.setSex(0);
        userInfo.setPassword(SecurityUtils.encryptPassword(userInfo.getPassword()));
        userInfoMapper.insert(userInfo);
        return true;
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin)
    {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        userInfoMapper.updateById(userInfo);
        return true;
    }

}
